const { defineConfig } = require('@vue/cli-service')
const BodyParser = require('body-parser')
console.log('process.env.NODE_ENV:', process.env.NODE_ENV)
module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: process.env.NODE_ENV === 'production' ? '/dianping/' : '/',
  devServer: {
    // 接口代理
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        pathRewrite: {
          '^/api': '',
        },
      },
    },
    // 模拟接口数据
    setupMiddlewares(middlewares, devServer) {
      devServer.app.use(BodyParser.json())

      devServer.app.get('/api/get-test', (req, res) => {
        res.json({ code: 0, msg: 'success', data: req.query })
      })

      devServer.app.post('/api/get-test', (req, res) => {
        res.json({ code: 0, msg: 'success', data: req.body })
      })

      return middlewares
    },
  },
})
